package com.test.springCloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/hystrixX")
public class HystrixXController {

    //fallbackMethod是降级的方法名，表示异常了由该方法进行兜底
    @HystrixCommand(fallbackMethod = "paymentOkFallbackHandler", commandProperties = {
            //value表示当前线程的超时时间为3s
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
    @RequestMapping("/hystrixOk")
    public Map<String, Object> paymentOk(@RequestParam("userId") Integer userId) throws InterruptedException{
        //休眠时间
//        Thread.sleep(5000);
        int a = 10 /0;
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("code", 200);
        resultMap.put("message", "hystrixOk调用成功");
        resultMap.put("userId", userId);
        return resultMap;
    }

    //方法入参和返回参数都必须与被服务降级的方法一致
    public Map<String, Object> paymentOkFallbackHandler(Integer userId) {
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("code", 200);
        resultMap.put("message", "hystrixOk超时，服务降级了");
        resultMap.put("userId", userId);
        return resultMap;
    }
}
